﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.Feiyong.Part
{
    /// <summary>
    /// Pay.xaml 的交互逻辑
    /// </summary>
    public partial class PmPay : UserControl
    {
        ViewModel.WuyeViewModel vm;
        SqlSugar.SqlSugarClient DB = new Model.DataContext().DB;
        public PmPay()
        {
            InitializeComponent();

            vm = DataContext as  ViewModel.WuyeViewModel;
            vm.CurrFeeBill = new Model.PmFeeBill();

            LoadData();
        }


        public void LoadData()
        {

            var now = DateTime.Now;
            //获取当前月的第一天
            DateTime d1 = new DateTime(now.Year, now.Month, 1);

            //获取当前月的最后一天

            DateTime d2 = d1.AddMonths(1).AddDays(-1);

            dtp_Start.SelectedDate = d1;
            dtp_End.SelectedDate = d2;

        }


        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {

            if(cmb_Room.SelectedItem==null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择房屋！");
                return;
            }

            if (dtp_Start.SelectedDate == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择缴费开始日期！");
                return;
            }
            if (dtp_End.SelectedDate == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择缴费结束日期！");
                return;
            }

            if(dtp_End.SelectedDate <dtp_Start.SelectedDate)
            {
                HandyControl.Controls.Growl.WarningGlobal("结束日期不能小于开始日期！");
                return;
            }

            vm.CurrRoom = cmb_Room.SelectedItem as Model.Rooms;

            //检查是否交过
            var startDate = dtp_Start.SelectedDate;
            var endDate = dtp_End.SelectedDate;

            var count =  DB.Queryable<Model.PmFeeBill>().Where(f=>f.RoomId ==vm.CurrRoom.Id &&( startDate >=f.BillingEndTime && startDate<=f.BillingStartTime)).Count();

            if(count>0)
            {
                HandyControl.Controls.Growl.WarningGlobal("您选择的日期冲突；请重新选择日期！");
                return;
            }


         
            try
            {
                //保存费用



                vm.CurrFeeBill.RoomId = vm.CurrRoom.Id;
                vm.CurrFeeBill.RoomNo = vm.CurrRoom.No;

                vm.CurrFeeBill.Date = DateTime.Now;

                vm.CurrFeeBill.UserId = Comm.TempData.LoginUser.Id;
                vm.CurrFeeBill.UserName = Comm.TempData.LoginUser.Name;
                vm.CurrFeeBill.PayType = cmb_FeeType.Text;

                  
             
              
                DB.Insertable<Model.PmFeeBill>(vm.CurrFeeBill).ExecuteCommand();
                vm.CurrRoom.PMFeeCollectTime = DateTime.Now ;
                DB.Updateable<Model.Rooms>(vm.CurrRoom).ExecuteCommand();

                vm.ListFeeBill =  DB.Queryable<Model.PmFeeBill>().ToList();
                vm.Dialog.Close();
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.FatalGlobal(ex.Message);
                 
            }
           
        }

        private void Btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            vm.Dialog.Close();
            //MessageBox.Show(txt_ShishouAmount.Value.ToString());
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            vm.ListFeeTypes = DB.Queryable<Model.FeeType>().ToList();
        }

        private void cmb_Build_DropDownOpened(object sender, EventArgs e)
        {
            var list = DB.Queryable<Model.Building>().ToList();
            cmb_Build.ItemsSource = list;
            cmb_Build.DisplayMemberPath = "Name";
            cmb_Build.SelectedValuePath = "Id";
        }

        private void cmb_Room_DropDownOpened(object sender, EventArgs e)
        {
            if(cmb_Build.SelectedValue!=null)
            {
                var bid = (int)cmb_Build.SelectedValue;
                vm.ListRoom = DB.Queryable<Model.Rooms>().Where(r => r.BuildingId == bid).ToList() ;

                cmb_Room.ItemsSource = vm.ListRoom;
                cmb_Room.SelectedValuePath = "Id";
                cmb_Room.DisplayMemberPath = "No";
            }
        }
        decimal price = 0;
        private void cmb_Room_DropDownClosed(object sender, EventArgs e)
        {

            if (cmb_Room.SelectedItem == null)
            {
                HandyControl.Controls.Growl.WarningGlobal("请选择房屋！");
                return;
            }

            vm.CurrRoom = cmb_Room.SelectedItem as Model.Rooms;
            if(vm.CurrRoom!=null && vm.CurrRoom.Floorage!=null)
            {
                vm.CurrFeeBill.RoomArea =(decimal) vm.CurrRoom.Floorage;

                //收费标准
                  //price = (decimal)DB.Queryable<Model.Costs>().Where(c => c.Name == "物业费").First().Price;
                //vm.CurrFeeBill.Rate =(decimal) price;
                //vm.CurrFeeBill.YingshouAmount = vm.CurrFeeBill.RoomArea * vm.CurrFeeBill.Rate;
            }

           
            


        }

        private void dtp_End_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            //    if(vm==null)
            //    {
            //        return;
            //    }
            //    if(vm.CurrFeeBill !=null && dtp_End.SelectedDate!=null)
            //    {
            //        vm.CurrFeeBill.YingshouAmount = vm.CurrFeeBill.RoomArea * vm.CurrFeeBill.Rate * ((DateTime)dtp_End.SelectedDate - (DateTime)dtp_Start.SelectedDate).Days/30;
            //    }
            //
        }
    }
}
